package AssetInventory

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/utils/result"
	"property_service/client/inventory_service"

	"property_api/internal/svc"
	"property_api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PagesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PagesLogic {
	return &PagesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Pages 查询资产库存的分页逻辑
func (l *PagesLogic) Pages(req *types.AssetInventoryPageReq) (resp *types.PageRes, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)

	// 构建查询条件对象
	where := &inventory_service.AssetInventory{
		TenantID:          tenantID,              // 租户ID
		ID:                req.ID,                // 记录ID
		AssetCode:         req.AssetCode,         // 资产编号
		AssetName:         req.AssetName,         // 资产名称
		AssetCategoryID:   req.AssetCategoryID,   // 资产编号id
		AssetCategory:     req.AssetCategory,     // 资产类别
		SpecModel:         req.SpecModel,         // 规格型号
		Unit:              req.Unit,              // 单位
		Quantity:          req.Quantity,          // 库存数量
		UseNum:            req.UseNum,            // 已使用数量
		SafetyStock:       req.SafetyStock,       // 安全库存
		StorageLocation:   req.StorageLocation,   // 存放地点
		PurchasePrice:     req.PurchasePrice,     // 采购价格
		Supplier:          req.Supplier,          // 供应商
		SupplierID:        req.SupplierID,        // 供应商id
		PurchaseDate:      req.PurchaseDate,      // 购置日期
		UsingDept:         req.UsingDept,         // 使用科室
		UsingDeptID:       req.UsingDeptID,       // 使用科室id
		StatusID:          req.StatusID,          // 状态ID
		StatusName:        req.StatusName,        // 采购状态
		AssetID:           req.AssetID,           // 资产ID
		InventoryStatus:   req.InventoryStatus,   // 状态
		StockID:           req.StockID,           // 仓库ID
		CountryOfOrigin:   req.CountryOfOrigin,   //产地
		CountryOfOriginID: req.CountryOfOriginID, //产地ID
		Brand:             req.Brand,             //品牌
		BrandID:           req.BrandID,           //品牌ID
		ManufacturersID:   req.ManufacturersID,   //生产商ID
		ManufacturersName: req.ManufacturersName, //生产商
	}

	// 调用服务层方法，查询资产库存记录的分页数据
	// 服务层方法 AssetInventoryPage 会处理实际的数据库查询操作
	data, err := l.svcCtx.InventoryService.AssetInventoryPage(l.ctx, &inventory_service.AssetInventoryPageReq{
		Page:  req.Page,  // 当前页码
		Limit: req.Limit, // 每页条数
		Where: where,     // 查询条件
	})

	// 返回分页数据的响应
	return result.SucPage(data, types.AssetInventory{}).PageEd()
}
